﻿using System.Web.Http;
using Pay.DTO;
using Pay.TO;
using Flurl.Http;
using Pay.Core;
using System.Threading.Tasks;
using System.Linq;

namespace Pay.Api.Controllers {
	/// <summary>
	/// 短信接口
	/// </summary>
	[RoutePrefix("sms")]
	public partial class SmsController : ApiController {
		[Route("send"), AllowAnonymous]
		public async Task<ResultMessage<bool>> Send([FromBody]send_model2 model) {
			if (model.mobile_code == "86") {
				//var data = new {
				//	username = "qxwp@xmsfbkj", password = SecureHelper.MD5("qWmArp8e"), to = model.mobile,
				//	text = model.text, encode = 1, msgType = 1, version = "1.0"
				//};
				var data=new{uid="1842", pwd =SecureHelper.MD5("coingold123456"),mobile = model.mobile,content=model.text};
				var api_result = await "http://www.467890.com/Admin/index.php/Message/send".PostJsonAsync(data).ReceiveString();
				var sms_result = api_result.ToObject<sms_result>();
				var msg = GetSmsError(int.Parse(sms_result.respCode));
				if (msg == "0")
					return ResultToJson.Json("发送成功", true);
				return ResultToJson.Json<bool>(msg);
			} else {
				var url = string.Format("http://api2.santo.cc/submit?command=MT_REQUEST&cpid=ksxcyv&cppwd=Q5FloqaL&da={0}{1}&sm={2}",
										model.mobile_code, model.mobile.TrimStart('0'), model.text);
				var api_result = url.GetStringAsync().Result;
				if (api_result.Split('=').Last() == "000")
					return ResultToJson.Json("发送成功", true);
				return ResultToJson.Json<bool>("发送失败");
			}
		}

		private string GetSmsError(int result) {
			string error = "未知错误";
			switch (result) {
				case 0:
					error = "0";
					break;
				case -2:
					error = "发送参数填定不正确";
					break;
				case -3:
					error = "用户载入延迟";
					break;
				case -6:
					error = "密码错误";
					break;
				case -7:
					error = "用户不存在";
					break;
				case -11:
					error = "发送号码数理大于最大发送数量";
					break;
				case -12:
					error = "余额不足";
					break;
				case -99:
					error = "内部处理错误";
					break;
			}
			return error;
		}
	}
}
